package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.BusinessSubscribeEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BusinessOpportunitySubscriptionAdapter extends BaseRecycleAdapter<BusinessSubscribeEntity> {

    private TextView tv_offer;
    private TextView tv_look;
    private IButton iButton;
    String areaCode;

    public BusinessOpportunitySubscriptionAdapter(Context context, List<BusinessSubscribeEntity> datas, String areaCode) {
        super(context, R.layout.item_business_opportunity_subscription, datas);
        this.areaCode = areaCode;
    }

    @Override
    public void convert(ItemViewHolder holder, final BusinessSubscribeEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView ivTag = holder.itemView.findViewById(R.id.iv_tag);
        tv_offer = holder.itemView.findViewById(R.id.tv_offer);
        tv_look = holder.itemView.findViewById(R.id.tv_look);
        TextView tvorder = holder.itemView.findViewById(R.id.tv_order);
//        tv_offer.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                iButton.offer(entity.getNo());
//            }
//        });
//        tv_look.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                iButton.look(entity);
//            }
//        });
//        holder.itemView.findViewById(R.id.rl_title).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                getUrl(entity.getNo());
//
//            }
//        });
//
//        holder.itemView.findViewById(R.id.tv_ding_dan).setOnClickListener(new View.OnClickListener() {//下单
//            @Override
//            public void onClick(View v) {
//                iButton.order(entity.getNo(),entity.getFreight());
//              //  OverbookingPublishActivity.start(context,"2");
//            }
//        });
//        tvorder.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                getH5Url(entity.getNo());
//
//            }
//        });
        switch (entity.getState()) {

            case 1:
                holder.itemView.findViewById(R.id.tv_offer).setVisibility(View.GONE);
                holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.GONE);

                if (!TextUtils.isEmpty(entity.getOrderNo())) {
                    tvorder.setText(entity.getOrderNo());
                    tvorder.setVisibility(View.VISIBLE);
                }
                ivTag.setImageResource(R.mipmap.img_tag2);
                break;
            case 2:
                holder.itemView.findViewById(R.id.tv_offer).setVisibility(View.GONE);
                holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.GONE);
                ivTag.setImageResource(R.mipmap.img_tag4);
                break;
        }
        if (entity.getPattern().equals("0") && entity.getState() == 0) {//普通模式，待接单
            ivTag.setImageResource(R.mipmap.img_offer_order);
            holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.VISIBLE);
            holder.itemView.findViewById(R.id.tv_offer).setVisibility(View.GONE);
            tv_look.setVisibility(View.GONE);
        } else if (entity.getPattern().equals("1") && entity.getState() == 0) {//竞价模式，竞价中
            ivTag.setImageResource(R.mipmap.img_tag1);
            holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.GONE);
            holder.itemView.findViewById(R.id.tv_offer).setVisibility(View.VISIBLE);
            if (entity.getOfferStatus().equals("1")) {
                tv_look.setVisibility(View.VISIBLE);
            } else {
                tv_look.setVisibility(View.GONE);
            }
        }
    }

    public interface IButton {
        void offer(String no);

        void look(BusinessSubscribeEntity subscribeEntity);

        void order(String no, String feight);
    }

    public void onButtonClick(IButton iButton) {
        this.iButton = iButton;
    }

    private void showUrl(String url) {
        WebViewX5HostActivity.start(context, url);
    }

    // 详情       货主订单详情 参数  goodsOrderDetails      货源详情参数   sourceDetails     ownerDetails
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("goodsOrderDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test", "3-------------" + response.body().getData().getUrl() + "       " + response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    Log.e("madong_test", "4=====" + url);
                    showUrl(url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUrl(String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getUrlSetting("sourceDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&areaCode=" + areaCode;
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
